package common

import (
	"time"
)

type IDGenerator interface {
	// Next 生成下一个ID序列
	Next() uint64
}

type idGenerator struct {
	memberID   uint16
	unixSecond int64
	generator  *EtcdIDGenerator
}

func (i *idGenerator) Next() uint64 {
	now := time.Now()
	if now.Unix() != i.unixSecond {
		i.unixSecond = now.Unix()
		i.generator = NewEtcdIDGenerator(i.memberID, now)
	}
	return i.generator.Next()
}

func NewGenerator(memberID uint16, now time.Time) IDGenerator {
	return &idGenerator{
		memberID:   memberID,
		unixSecond: now.Unix(),
		generator:  NewEtcdIDGenerator(memberID, now),
	}
}
